/**
 * inforcreation.com co.Ltd
 * MichaelHuang
 * 2011-12-16
 */
package com.zchat.android.service_aidl_beans;

import java.io.InputStream;
import java.util.List;
import java.util.Map;

import org.jivesoftware.smack.ChatManager;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;

import com.zchat.android.R;
import com.zchat.android.WelcomeActivity;
import com.zchat.android.app.LauncherApplication;
import com.zchat.api.ZChat;
import com.zchat.api.log.Logger;
import com.zchat.api.modules.ModuleManager;
import com.zchat.api.modules.chat.SingleChatMessageManager;
import com.zchat.api.protocal.xml.TestXMLProtocal;
import com.zchat.api.util.ZChatSetting;

/**
 * default application service.
 * @author Oliverzhu
 *	2012/6/26
 */
public class ZChatService extends Service {
	private static final String TAG = "ZChatService";
	private Context context;
	
	protected static final boolean DEBUG = ZChatSetting.DEBUG;
	
	@Override
	public void onCreate() {
		super.onCreate();
		
		context = this;
		
		if(DEBUG) Logger.info(TAG+"-Start application service.");
		 
       
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
//		Logger.debug("Destory application service.");
//		AlarmAlertWakeLock.releaseCpuLock();
	}
	
	@SuppressWarnings("unused")
	private IApplicationService.Stub stub = new IApplicationService.Stub() {
		
		@Override
		public Map test(List<String> params) throws RemoteException {
			Map map = null;
			try {
				//
				InputStream in = context.getResources().openRawResource(R.raw.test);
//						//ZChatHttpHandler.sendRequest("");
//				File file = new File("sdcard\\test.xml");
//				InputStream in = new FileInputStream(file);
				map = TestXMLProtocal.getInstance().parse(in);
				
				in.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return map;
		}
		

	};
	
	@Override
	public IBinder onBind(Intent intent) {
		if(DEBUG) Logger.info(TAG+"-on bind.");
		return stub;
	}

	/* (non-Javadoc)
	 * @see android.app.Service#onStart(android.content.Intent, int)
	 */
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		if(DEBUG) Logger.debug(TAG+"-messageservice start");
		LauncherApplication lauApplication = (LauncherApplication) getApplication();
		
		ZChat zchat = lauApplication.getZChat(getApplicationContext());
		
		ModuleManager moduleManager = zchat.getModuleManager();
		
		ChatManager manager = zchat.getXMPPConnection().getChatManager();
		
		SingleChatMessageManager chatMessageManager = 
				(SingleChatMessageManager) moduleManager.getModule(SingleChatMessageManager.class);
		manager.addChatListener(chatMessageManager);
		
		return START_STICKY;
	}

	
}
